package xju.springboot13_14.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import xju.springboot13_14.entity.Book;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("user")
public class UserController {
    @Value("${friend.name}")
    private String name;
    @Value("${friend.age}")
    private String age;

    @Value("${pets[1]}")
    private String pet;

    @Autowired
    private Environment environment;

    @Autowired
    private Friend friend;

    @GetMapping
    public String index() {
        System.out.println("name:" + name);
        System.out.println("age:" + age);
        System.out.println("pet:" + pet);
        System.out.println("index");

        System.out.println(environment.getProperty("friend.name"));
        System.out.println(environment.getProperty("friend.age"));
        System.out.println(environment.getProperty("pets[1]"));

        System.out.println(friend);
        return "index";
    }

    @GetMapping("jsp")
    public String index2() {
        System.out.println("123");
        return "list";
    }

    @GetMapping("html")
    public String html(Model model) {
        System.out.println(33);
        model.addAttribute("price", 9.9);
        model.addAttribute("str", "字符串");
        Book book = new Book();
        book.setId(1);
        book.setTitle("西游记");
        book.setPrice(99.9);
        book.setAuthor("wu");

        Book bookOne = new Book();
        bookOne.setId(2);
        bookOne.setTitle("水浒传");
        bookOne.setPrice(30.0);
        bookOne.setAuthor("luo");
        List<Book> bookList = new ArrayList<>();
        bookList.add(book);
        bookList.add(bookOne);
        model.addAttribute("book", book);
        model.addAttribute("red", "red");
        model.addAttribute("books", bookList);
        return "test";
    }
}
